/**
 *******************************************************************************************************************************************
 * @file        setup_home_page_pp.c
 * @brief       GIF 演示、中文字体应用页
 * @details     /
 * @since       Change Logs:
 * Date         Author       Notes
 * 2022-12-08   lzh          the first version
 * 2022-12-21   lzh          重构
 * 2023-02-23   lzh          精简注释
 * @remark      /
 *******************************************************************************************************************************************
 * @attention
 *
 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH CODING INFORMATION
 * REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS A RESULT, SYNWIT SHALL NOT BE HELD LIABLE
 * FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
 * OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN IN CONN-
 * -ECTION WITH THEIR PRODUCTS.
 *
 * @copyright   2012 Synwit Technology
 *******************************************************************************************************************************************
 */
#include "ui.h"
#include "ui_event.h"

/* CE-48Pin 只有 2MB SDRAM, 但分辨率较低, 因此可以进行字体滚动显示 */
#if (SWM34S_LCM_PCBV == SWM34SCE_PIN48_A2)
	/** 顶部字体是否滚动(0 : 不滚动        1 : 保持大小循环滚动) */
	#define FONT_SCROLL_TOP         1

#else /* 分辨率较高 or RGB888 时, 同时在一个页面内进行 视频播放、GIF轮播、字体滚动 三种效果时, 会引起闪烁、抖动 */

	/** 顶部字体是否滚动(0 : 不滚动        1 : 保持大小循环滚动) */
	#define FONT_SCROLL_TOP         ( ( (LCD_HDOT >= 800 && LCD_VDOT >= 480) || (LCD_FMT == LCD_ARGB888) ) ? 0 : 1 )
#endif

/** 中文字体样式 */
lv_style_t Style_Label_China;

static void setup_style(lv_ui *ui);

/**
 * @brief   创建 Picture 页
 * @param   ui : 全局句柄
 * @retval  /
 */
void setup_home_page_pp(lv_ui *ui)
{
    // Write codes scr_home_tab_pp_imgbtn_home
    ui->scr_home_tab_pp_imgbtn_home = lv_imgbtn_create(ui->scr_home_tab_pp, NULL);
    lv_obj_set_pos(ui->scr_home_tab_pp_imgbtn_home,
                   GET_X(0, 0, ICON_HB_W, GET_OFFSET_X(LV_HOR_RES_MAX, ICON_HB_W, 3)),
                   ICON_HB_H);
    lv_obj_set_size(ui->scr_home_tab_pp_imgbtn_home,
                    ICON_HB_W,
                    ICON_HB_H);

    lv_imgbtn_set_src(ui->scr_home_tab_pp_imgbtn_home, LV_BTN_STATE_RELEASED, Image_Table[ICON_IMG_START + 16].img);
    lv_imgbtn_set_src(ui->scr_home_tab_pp_imgbtn_home, LV_BTN_STATE_PRESSED, Image_Table[ICON_IMG_START + 16].img);
    lv_imgbtn_set_src(ui->scr_home_tab_pp_imgbtn_home, LV_BTN_STATE_CHECKED_RELEASED, Image_Table[ICON_IMG_START + 16].img);
    lv_imgbtn_set_src(ui->scr_home_tab_pp_imgbtn_home, LV_BTN_STATE_CHECKED_PRESSED, Image_Table[ICON_IMG_START + 16].img);

    // Write codes scr_home_tab_pp_imgbtn_back
    ui->scr_home_tab_pp_imgbtn_back = lv_imgbtn_create(ui->scr_home_tab_pp, NULL);
    lv_obj_set_pos(ui->scr_home_tab_pp_imgbtn_back,
                   GET_X(2, 0, ICON_HB_W, GET_OFFSET_X(LV_HOR_RES_MAX, ICON_HB_W, 3)),
                   ICON_HB_H);
    lv_obj_set_size(ui->scr_home_tab_pp_imgbtn_back,
                    ICON_HB_W,
                    ICON_HB_H);

    lv_imgbtn_set_src(ui->scr_home_tab_pp_imgbtn_back, LV_BTN_STATE_RELEASED, Image_Table[ICON_IMG_START + 17].img);
    lv_imgbtn_set_src(ui->scr_home_tab_pp_imgbtn_back, LV_BTN_STATE_PRESSED, Image_Table[ICON_IMG_START + 17].img);
    lv_imgbtn_set_src(ui->scr_home_tab_pp_imgbtn_back, LV_BTN_STATE_CHECKED_RELEASED, Image_Table[ICON_IMG_START + 17].img);
    lv_imgbtn_set_src(ui->scr_home_tab_pp_imgbtn_back, LV_BTN_STATE_CHECKED_PRESSED, Image_Table[ICON_IMG_START + 17].img);

    // 顶部标语
    ui->scr_home_tab_pp_label_top = lv_label_create(ui->scr_home_tab_pp, NULL);
    lv_label_set_recolor(ui->scr_home_tab_pp_label_top, true); // 允许重新着色
    lv_label_set_text(ui->scr_home_tab_pp_label_top,
                      "#daa520 广东省# #3CB371 佛山市# #0000ff 南海区桂城街道# #ff0000 广东华芯微特集成电路有限公司#");

    #if FONT_SCROLL_TOP
    lv_label_set_long_mode(ui->scr_home_tab_pp_label_top, LV_LABEL_LONG_SROLL_CIRC);
    // LV_LABEL_LONG_SROLL_CIRC - 保持大小并循环滚动文本
    // LV_LABEL_LONG_SROLL      - 保持大小并来回滚动文本
    #endif

    lv_obj_set_size(ui->scr_home_tab_pp_label_top,
                    GET_X(2, 0, ICON_HB_W, GET_OFFSET_X(LV_HOR_RES_MAX, ICON_HB_W, 3)) - GET_X(0, 0, ICON_HB_W, GET_OFFSET_X(LV_HOR_RES_MAX, ICON_HB_W, 3)) - ICON_HB_W,
                    ICON_HB_H * 9 / 10);

    lv_obj_align(ui->scr_home_tab_pp_label_top, NULL, LV_ALIGN_IN_TOP_MID, 0,
                 (GET_X(2, 0, ICON_HB_W, GET_OFFSET_X(LV_HOR_RES_MAX, ICON_HB_W, 3)) - GET_X(0, 0, ICON_HB_W, GET_OFFSET_X(LV_HOR_RES_MAX, ICON_HB_W, 3)) - ICON_HB_W > lv_obj_get_width(ui->scr_home_tab_pp_label_top))
                     ? ICON_HB_H
                     : 0); // 相对父级顶部居中对齐

    // 中间介绍文本
    ui->scr_home_tab_pp_label_mid = lv_label_create(ui->scr_home_tab_pp, NULL);
    lv_label_set_recolor(ui->scr_home_tab_pp_label_mid, true); // 允许重新着色
    lv_label_set_text(ui->scr_home_tab_pp_label_mid, "#ff0000 3.3V供电系列:# SWM181 SWM190 SWM320 SWM341 \n");
    lv_label_ins_text(ui->scr_home_tab_pp_label_mid, LV_LABEL_POS_LAST, "#ff0000 5V供电系列:# SWM201 SWM260 \n"); // 在指定位置追加文本
    lv_label_ins_text(ui->scr_home_tab_pp_label_mid, LV_LABEL_POS_LAST, "#0000ff M0内核系列:# SWM181 SWM190 SWM201 SWM260 \n");
    lv_label_ins_text(ui->scr_home_tab_pp_label_mid, LV_LABEL_POS_LAST, "#0000ff M4内核系列:# SWM320 SWM341 \n");
    lv_label_ins_text(ui->scr_home_tab_pp_label_mid, LV_LABEL_POS_LAST, "#daa520 TFT-LCD显示应用:# SWM19S SWM32S \n");
    lv_label_ins_text(ui->scr_home_tab_pp_label_mid, LV_LABEL_POS_LAST, "#daa520 SLCD显示应用:# SWM181 \n");
    lv_label_ins_text(ui->scr_home_tab_pp_label_mid, LV_LABEL_POS_LAST, "#20B2AA 电机应用:# SWM190 SWM201 \n");
    lv_label_ins_text(ui->scr_home_tab_pp_label_mid, LV_LABEL_POS_LAST, "#20B2AA 大家电应用:# SWM181 SWM260");
    lv_obj_align(ui->scr_home_tab_pp_label_mid, NULL, LV_ALIGN_CENTER, 0, 0); // 设置相对位置

    // Write codes scr_home_tab_pp_imgbtn_gif[]
    for (uint8_t i = 0; i < sizeof(ui->scr_home_tab_pp_imgbtn_gif) / sizeof(ui->scr_home_tab_pp_imgbtn_gif[i]); ++i)
    {
        ui->scr_home_tab_pp_imgbtn_gif[i] = lv_imgbtn_create(ui->scr_home_tab_pp, NULL);
        lv_obj_set_pos(ui->scr_home_tab_pp_imgbtn_gif[i],
                       GET_X(i, 0, ICON_PP_W, GET_OFFSET_X(LV_HOR_RES_MAX, ICON_PP_W, 3)),
                       LV_VER_RES_MAX - ICON_PP_H / 2*3);//4 * 5);
        lv_obj_set_size(ui->scr_home_tab_pp_imgbtn_gif[i],
                        ICON_PP_W,
                        ICON_PP_H);

        lv_imgbtn_set_src(ui->scr_home_tab_pp_imgbtn_gif[i], LV_BTN_STATE_RELEASED, Image_Table[ICON_IMG_START + 13 + i].img);
        lv_imgbtn_set_src(ui->scr_home_tab_pp_imgbtn_gif[i], LV_BTN_STATE_PRESSED, Image_Table[ICON_IMG_START + 13 + i].img);
        lv_imgbtn_set_src(ui->scr_home_tab_pp_imgbtn_gif[i], LV_BTN_STATE_CHECKED_RELEASED, Image_Table[ICON_IMG_START + 13 + i].img);
        lv_imgbtn_set_src(ui->scr_home_tab_pp_imgbtn_gif[i], LV_BTN_STATE_CHECKED_PRESSED, Image_Table[ICON_IMG_START + 13 + i].img);
    }

    // Write codes scr_home_tab_pp_btn_hide[]
    for (uint8_t i = 0; i < sizeof(ui->scr_home_tab_pp_btn_hide) / sizeof(ui->scr_home_tab_pp_btn_hide[i]); ++i)
    {
        ui->scr_home_tab_pp_btn_hide[i] = lv_btn_create(ui->scr_home_tab_pp, NULL);
        lv_obj_set_size(ui->scr_home_tab_pp_btn_hide[i],
                        (LV_HOR_RES_MAX - FLASH_VIDEO_AREA_W) / 2,
                        lv_obj_get_height(ui->scr_home_tab_pp_label_mid));

        lv_obj_align(ui->scr_home_tab_pp_btn_hide[i], NULL,
                     (i & 0x1) ? LV_ALIGN_IN_RIGHT_MID : LV_ALIGN_IN_LEFT_MID,
                     0, 0); // 相对父级内部 靠 右/左 居中对齐
    }

    // Write codes scr_home_tab_pp_img_gif

    // Set Style for scr page
    setup_style(ui);

    // Init events for scr page
    events_init_home_page_pp(ui);

    // 暂未加载字库, 故先隐藏字体显示
    lv_obj_set_hidden(ui->scr_home_tab_pp_label_top, true);
    lv_obj_set_hidden(ui->scr_home_tab_pp_label_mid, true);
}

static void setup_style(lv_ui *ui)
{
    // 添加图标样式
    lv_obj_add_style(ui->scr_home_tab_pp_imgbtn_home, LV_IMGBTN_PART_MAIN, &Style_Common_Btn);
    lv_obj_add_style(ui->scr_home_tab_pp_imgbtn_back, LV_IMGBTN_PART_MAIN, &Style_Common_Btn);

    // Write codes scr_home_tab_pp_imgbtn_gif[]
    for (uint8_t i = 0; i < sizeof(ui->scr_home_tab_pp_imgbtn_gif) / sizeof(ui->scr_home_tab_pp_imgbtn_gif[i]); ++i)
    {
        lv_obj_add_style(ui->scr_home_tab_pp_imgbtn_gif[i], LV_IMGBTN_PART_MAIN, &Style_Common_Btn);
    }

    // 隐藏式按钮的样式
    static lv_style_t Style_Hide;
    lv_style_init(&Style_Hide);
    lv_style_set_bg_opa(&Style_Hide, LV_STATE_DEFAULT, LV_OPA_TRANSP);         // 指定背景的不透明度
    lv_style_set_border_color(&Style_Hide, LV_STATE_DEFAULT, LV_COLOR_BLACK);  // 指定边框的颜色 -黑色 LV_COLOR_WHITE / 白色 LV_COLOR_BLACK
    lv_style_set_border_opa(&Style_Hide, LV_STATE_DEFAULT, LV_OPA_TRANSP);     // 指定边框的不透明度
    lv_style_set_border_width(&Style_Hide, LV_STATE_DEFAULT, 0);               // 设置边框的宽度
    lv_style_set_outline_color(&Style_Hide, LV_STATE_DEFAULT, LV_COLOR_BLACK); // 指定轮廓的颜色 -黑色 LV_COLOR_WHITE / 白色 LV_COLOR_BLACK
    lv_style_set_outline_opa(&Style_Hide, LV_STATE_DEFAULT, LV_OPA_TRANSP);    // 指定轮廓的不透明度
    lv_style_set_outline_width(&Style_Hide, LV_STATE_DEFAULT, 0);              // 设置轮廓的宽度
    lv_style_set_outline_pad(&Style_Hide, LV_STATE_DEFAULT, 0);                // 设置对象与轮廓之间的间距

    // Write codes scr_home_tab_pp_btn_hide[]
    for (uint8_t i = 0; i < sizeof(ui->scr_home_tab_pp_btn_hide) / sizeof(ui->scr_home_tab_pp_btn_hide[i]); ++i)
    {
        lv_obj_add_style(ui->scr_home_tab_pp_btn_hide[i], LV_BTN_PART_MAIN, &Style_Hide);
    }

    // 中文字体样式
    // static lv_style_t Style_Label_China;
    lv_style_init(&Style_Label_China);
    // lv_style_set_text_font(&Style_Label_China, LV_STATE_DEFAULT, &USER_FONT);     // 字体样式-中文字库
    lv_style_set_bg_opa(&Style_Label_China, LV_STATE_DEFAULT, LV_OPA_COVER);     // 背景-完全不透明
    lv_style_set_bg_color(&Style_Label_China, LV_STATE_DEFAULT, LV_COLOR_WHITE); // 背景-白色

    // 添加字体样式
    lv_obj_add_style(ui->scr_home_tab_pp_label_top, LV_LABEL_PART_MAIN, &Style_Label_China);
    lv_obj_add_style(ui->scr_home_tab_pp_label_mid, LV_LABEL_PART_MAIN, &Style_Label_China);
}
